import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

/**
 * Main Portion of the solver. Invoked directly.
 * 
 * @author Mark Acosta
 */

public class ReadHCPScanner{

	FileInputStream fStream;

	String name;
	String comment;
	String type;
	int dimension;
	String edge_weight_type;
	String edge_weight_format;
	String edge_weight_section;
	Node[] sequence;
	
	double bestCost = 0;
	static boolean verbose;

	/**
	 * Parses file and populates fields. Does not actually solve the HCP
	 * 
	 * @param inputFile
	 *            File to be used as input for the Solver
	 * @throws FileNotFoundException
	 *             if input file does not exist, or output file cannot be
	 *             created
	 * @throws IOException
	 *             if any I/O error occurs while reading
	 */
	public ReadHCPScanner(File inputFile) throws FileNotFoundException,IOException 
{
		
		Scanner reader = new Scanner(new FileReader(inputFile));

		boolean header = true;
		reader.useDelimiter("\\p{javaWhitespace}+:\\p{javaWhitespace}+|\r\n|\n");
			while (header)// checks for the fields before the file information and stores them
			{
				String curr = reader.next();
			// Read off Name Field
			if (curr.equals("NAME"))
			{
				name = reader.next();
			}

			// Read off Comment field
			else if (curr.equals("COMMENT"))
			{
				comment = reader.next();
			}

			// Read off Type field
			else if (curr.equals("TYPE")) 
			{
				type = reader.next();
			}

			// Read off Dimension field
			else if (curr.equals("DIMENSION")) 
			{
				dimension = reader.nextInt();
			}

			// Read off Edge Weight Type field
			else if (curr.equals("EDGE_WEIGHT_TYPE")) 
			{
				edge_weight_type = reader.next();
			}
			// Read off Edge Weight Data section
			else if (curr.equals("EDGE_DATA_SECTION")) 
			{
				header = false;
			}

			// Read off Edge data format
			else if (curr.equals("EDGE_DATA_FORMAT"))
			{
				edge_weight_section = reader.next();
			}

		}
		sequence = new Node[dimension + 1]; //initializes array with enough space so that the array index matches the actual index number
		
		for(int x = 0; x < dimension + 1; x ++)
		{
			sequence[x] = new Node(0, dimension + 1);
		}
		
		reader.useDelimiter("\\p{javaWhitespace}+|\r\n|\n"); //delimiter checks for whitespace and return values
		
		int idx1 = reader.nextInt();
		int idx2 = 0; //initializes but reads afterwards so doesnt pull null value below
		while(idx1 != -1)
		{
			idx2 = reader.nextInt();
			if(idx1 < idx2) // swaps the 2 elements without using a temporary variable
			{
				idx2 = idx2 ^ idx1;
				idx1 = idx1 ^ idx2;
				idx2 = idx2 ^ idx1;
			}
			
			sequence[idx1].index = idx1; // assigns node
			sequence[idx1].array[idx2].index = idx2; //assigns edge given the originating node
			
			idx1 = reader.nextInt();
		}
		reader.close();
}
	
	
	

	/**
	 * Invocation of the HCP reader, TSP writer and HCP writer
	 * 
	 * @param args
	 *            InputFile OutputFile [verbose]
	 * @throws IOException 
	 */

	public static void main(String[] args) throws IOException {

		
			File inputFile = new File(args[0]);
			File outFile = new File(args[1]);
			ReadHCPScanner r = new ReadHCPScanner(inputFile);
			new WriteTSP2(r.sequence,outFile,r);
		    //new WriteHCP(r,args); // uncomment out this field after you have created the tsp and ran it in corcorde with the follow args
			//args[0-2] original.hcp, desired tsp filename.tsp, desired hcp filename.hcp
			//args[4] dimension of the hcp file result list
			//args[5-dimension*2 +1] resulting information from concorde
		

		
	}

}
